package com.briup.srms.exam.web.controller;

import com.briup.srms.exam.bean.QuestionOptions;
import com.briup.srms.exam.bean.extend.QuestionWithScoreExtend;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.srms.exam.bean.Paper;
import com.briup.srms.exam.bean.extend.PaperExtend;
import com.briup.srms.exam.service.IPaperService;
import com.jacky.base.utils.Message;
import com.jacky.base.utils.MessageUtil;
import com.jacky.base.utils.PageVM;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @program: 杰普基础管理系统
 * @description: 试卷的控制器类
 * @author: charles
 * @create: 2021-1-14 11:09
 **/
@Api(description="试卷相关接口")
@Validated
@RestController
@RequestMapping("/exam/paper")
public class PaperController {
	@Autowired
	private IPaperService paperService;

	@ApiOperation(value = "修改试卷题目成绩")
	@PostMapping("edit")
	public Message edit(@RequestBody PaperExtend paper) {
        System.out.println(paper.getId());
        System.out.println(paper);
		paperService.edit(paper);
		return MessageUtil.success("更新成功");
	}

	@ApiOperation(value = "分页多条件获取数据字典信息")
	@GetMapping("pageQuery")
	@ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"), })
	public Message pageQuery(int page, int pageSize) {
		PageVM<Paper> pageVM = paperService.pageQuery(page, pageSize);
		return MessageUtil.success(pageVM);
	}

	@ApiOperation(value = "通过id删除")
	@ApiImplicitParams(@ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true))
	@GetMapping("deleteById")
	public Message deleteById(long id) {
		paperService.deleteById(id);
		return MessageUtil.success("删除成功");
	}

	@ApiOperation(value = "保存或更新消息信息", notes = "如果参数中包含id后端认为是更新操作，如果参数中不包含id认为是插入操作")
	@PostMapping("saveOrUpdate")
	public Message saveOrUpdate(Paper paper) {
		paperService.saveOrUpdate(paper);
		return MessageUtil.success("更新成功");
	}

	@ApiOperation(value = "组建试卷")
	@PostMapping("merge")
	public Message merge(@RequestBody PaperExtend paper) {
        System.out.println(paper);
		paperService.merge(paper);
		return MessageUtil.success("更新成功");
	}

	@ApiOperation(value = "通过ID查询试卷详情")
	@ApiImplicitParams(@ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true))
	@GetMapping("findById")
	public Message findById(long id) {
		PaperExtend paper = paperService.findById(id);
		return MessageUtil.success(paper);
	}

	@ApiOperation(value = "通过ID查询试卷详情-不携带答案")
	@ApiImplicitParams(@ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true))
	@GetMapping("findByIdWithoutAnswer")
	public Message findByIdWithoutAnswer(long id) {
		PaperExtend paper = paperService.findByIdWithoutAnswer(id);
		return MessageUtil.success(paper);
	}


	@ApiOperation(value = "导出word")
	@ApiImplicitParams(@ApiImplicitParam(name = "id", value = "试卷主键", paramType = "query", required = true))
	@GetMapping("exportPaper")
	public void exportPaper(HttpServletResponse response, long id) throws Exception {
		// 试卷
		PaperExtend paper = paperService.findById(id);
		String paper_name = paper.getName();
		List<QuestionWithScoreExtend> questions = paper.getQuestions();

		response.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(paper_name + ".docx", "utf-8"));
		OutputStream os = response.getOutputStream();
		XWPFDocument doc = new XWPFDocument();


		// 1. 设置试卷标题
		XWPFParagraph titleParagraph = doc.createParagraph();
		//设置段落居中
		titleParagraph.setAlignment(ParagraphAlignment.CENTER);
		XWPFRun titleParagraphRun = titleParagraph.createRun();
		titleParagraphRun.setText(paper_name);
		titleParagraphRun.setColor("000000");
		titleParagraphRun.setFontSize(20);

		// 2. 设置试卷问题
		for(int i=0;i<questions.size();i++){
			QuestionWithScoreExtend q = questions.get(i);
			XWPFParagraph p = doc.createParagraph();
			p.setAlignment(ParagraphAlignment.LEFT);
			XWPFRun run = p.createRun();
			run.setText((i+1)+"、"+"【"+q.getType()+"】"+q.getName());

			// 选项
			if(q.getType().equals("单选") ||q.getType().equals("多选")  ){
				for(QuestionOptions qos : q.getOptions()){
					XWPFParagraph option_p = doc.createParagraph();
					option_p.setAlignment(ParagraphAlignment.LEFT);
					XWPFRun option_run = option_p.createRun();
					option_run.setText("    "+qos.getLabel()+"、"+qos.getName());
				}
			}
		}


		doc.write(os);
		os.flush();
	}


}
